package org.hawk.gwtppc.webtest.tests.fieldsuite;

import java.lang.reflect.Field;

import org.hawk.gwtppc.webtest.common.Asserts;
import org.hawk.gwtppc.webtest.common.Test;
import org.hawk.gwtppc.webtest.tests.mock.ClassMock;
import org.hawk.gwtppc.webtest.tests.mock.GenericClassMock;
import org.hawk.gwtppc.webtest.tests.mock.GenericSubClassMock;
import org.hawk.gwtppc.webtest.tests.mock.SubClassMock1;
import org.hawk.gwtppc.webtest.tests.mock.SubClassMock2;

public class GetTest implements Test {

	public void run() throws Throwable {
		try {
			SubClassMock1.class.getDeclaredField("serialVersionUID").getLong(null);
			Asserts.fail("Unable to get final field");
		} catch (IllegalAccessException e) {
		}

		Field fld = SubClassMock1.class.getDeclaredField("serialVersionUID");
		fld.setAccessible(true);
		Asserts.assertEquals(fld.getLong(null), 7546163286061852009L);

		try {
			Field fldx = SubClassMock1.class.getDeclaredField("parent");
			fldx.setAccessible(true);
			fldx.get(null);
			Asserts.fail("Instance field cannot be get from NULL");
		} catch (NullPointerException ex) {
		}
		
		ClassMock parent = new SubClassMock2(2);
		SubClassMock1 mock = new SubClassMock1();
		Field fld2 = SubClassMock1.class.getDeclaredField("parent");
		fld2.setAccessible(true);
		Asserts.assertEquals(fld2.get(mock), null);

		fld2.set(mock, parent);
		Asserts.assertTrue(fld2.get(mock) == parent);
		
		GenericClassMock<?, ?, ?> mock2 = new GenericSubClassMock<String>();
		Field fld3 = GenericClassMock.class.getDeclaredField("genericField");
		fld3.setAccessible(true);
		Asserts.assertEquals(fld3.get(mock2), GenericSubClassMock.THE_STRING);
	}

	public String getName() {
		return "get()";
	}

}
